package com.newer.service.sys.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.newer.dao.sys.mapper.PickstandardMapper;
import com.newer.pojo.sys.Pickstandard;
import com.newer.pojo.sys.PickstandardExample;
import com.newer.pojo.sys.PickstandardExample.Criteria;
import com.newer.service.sys.iter.PickstandardService;
import com.newer.utils.sys.BootstrapDataTable;
import com.newer.utils.sys.Conditions;
import com.newer.utils.sys.IsNotNullUtil;
/**
 * 收派标准业务层
 * @author 杨海川
 *
 */
@Service
public class PickstandardServiceImpl implements  PickstandardService {

	@Autowired
	private PickstandardMapper pickstandardMapper;
	@Override
	public List<Pickstandard> selectByExample(PickstandardExample example) {
		// TODO Auto-generated method stub
		return pickstandardMapper.selectByExample(example);
	}

	@Override
	public BootstrapDataTable selectPickstandardByExample(
			BootstrapDataTable dataTable, Conditions conditions) {
		System.out.println("名称:"+conditions.getStandardName());
		
		PickstandardExample example = new PickstandardExample();
		Criteria createCriteria = example.createCriteria();
		createCriteria.andDeleteStatusEqualTo(0);
		// 设置初始排序规则
		System.out.println("数据:" + conditions.getSearch());
		if (IsNotNullUtil.isNotNull(example)) {
			example.setOrderByClause(dataTable.getSort() + "  " + dataTable.getOrder());
		}
		// 根据条件查询
		if(IsNotNullUtil.isNotNull(conditions)){
			if (IsNotNullUtil.isNotNull(conditions.getStandardName())) {
				createCriteria.andStandardNameLike("%"+conditions.getStandardName()+"%");
		}
		}
		

		// 设置分页条件
		PageHelper.offsetPage(dataTable.getOffset(), dataTable.getLimit());

		List<Pickstandard> pickstandardList = pickstandardMapper.selectByExample(example);
		PageInfo<Pickstandard> info = new PageInfo<Pickstandard>(pickstandardList);

		// 设置一页显示几条数据
		dataTable.setRows(info.getList());
		// 设置总共多少条记录数
		dataTable.setTotal(new Long(info.getTotal()).intValue());
        
		return dataTable;
	}
	/**
	 * 批量删除，实际把删除状态改为1
	 */
	@Override
	public int deletePickstandardByIds(String Ids) {
		// TODO Auto-generated method stub
		String[] split=Ids.split(",");
		//声明集合对象
		List<String> ids=null;
		if (IsNotNullUtil.isNotNull(split)) {
			ids=new  ArrayList<String>();
			for (String id : split) {
				ids.add(id);
			}
		}

		return pickstandardMapper.deleteByIds(ids);
	}
	/**
	 * 删除，修改删除状态为1
	 */
	@Override
	public int delete(Integer id) {
		Pickstandard pickstandard=new Pickstandard();
		pickstandard.setId(id);
		pickstandard.setDeleteStatus(1);
		return pickstandardMapper.updateByPrimaryKeySelective(pickstandard);
	}
	
	/**
	 * 添加一个收派标准
	 */
	@Override
	public int addPickstandard(Pickstandard pickstandard) {
		// TODO Auto-generated method stub
		pickstandard.setDeleteStatus(0);
		return pickstandardMapper.insert(pickstandard);
	}
	
	/**
	 * 检查是否存在收派标准
	 */
	@Override
	public Map<String, Boolean> selectPickstandardByStandardName(
			String standardName) {
		// TODO Auto-generated method stub
		System.out.println("收派标准名称"+standardName);
		boolean result = true;
		PickstandardExample example=  new PickstandardExample();
		Criteria  criteria=example.createCriteria();
		criteria.andStandardNameEqualTo(standardName);
		List<Pickstandard>list=pickstandardMapper.selectByExample(example);
		if(list.size()>0){
			result=false;
		}
		Map<String , Boolean>map=new HashMap<String,Boolean>();
		map.put("valid", result);
		return map;
	}
	/**
	 * 修改
	 */
	@Override
	public int updatePickstandardByPrimarykey(Pickstandard pickstandard) {
		// TODO Auto-generated method stub
		return pickstandardMapper.updateByPrimaryKeySelective(pickstandard);
	}
	
	/**
	 * 根据id查询
	 */
	@Override
	public Pickstandard selectPickstandardById(Integer id) {
		// TODO Auto-generated method stub
		Pickstandard selectByPrimarykey=pickstandardMapper.selectByPrimaryKey(id);
		return selectByPrimarykey;
	}
}
